package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaOrgIncomeCompanyDao;
import com.qymerge.manage.base.ro.MetaOrgIncomeCompanyRO;
import com.qymerge.manage.base.ro.MetaProfitReportItemDetail1RO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: MetaOrgIncomeCompanyDaoImpl</p>
 *<p>Description: 收入/成本的来源公司 </p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaOrgIncomeCompanyDaoImpl extends BaseDao implements IMetaOrgIncomeCompanyDao {


    private String dwd_MCRS_MDM_meta_OrgIncomeCompany = "SELECT oc.Id as Id, oc.OrgId as OrgId, oc.CompanyId as CompanyId,o.Id as OrgId, o.Name as OrgName,c.Id as CompanyID,c.Code as CompanyCode,c.Name as CompanyName " +
        "FROM dwd_MCRS_MDM_meta_OrgIncomeCompanyNew oc " +
        "left join dwd_MCRS_MDM_meta_Org o on oc.OrgId = o.Id " +
        "left join dwd_MCRS_MDM_meta_Company c on oc.CompanyId = c.Id WHERE 1=1 ";

    @Override
    public List list(MetaOrgIncomeCompanyRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_OrgIncomeCompany;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and oc.OrgId=:OrgId";
        paramMap.put("OrgId", ro.getOrgId());

        // 分页数据获取
        sql += " order by oc.Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        sql += " order by oc.VisOrder asc ";

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaOrgIncomeCompanyRO>(MetaOrgIncomeCompanyRO.class));
    }

    @Override
    public int total(MetaOrgIncomeCompanyRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_OrgIncomeCompanyNew where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and OrgId=:OrgId";
        paramMap.put("OrgId", ro.getOrgId());

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaOrgIncomeCompanyRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_OrgIncomeCompany);
        sql.append(" and oc.Id=?");
        RowMapper<MetaOrgIncomeCompanyRO> rowMapper = new BeanPropertyRowMapper<MetaOrgIncomeCompanyRO>(MetaOrgIncomeCompanyRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int insert(MetaOrgIncomeCompanyRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_meta_OrgIncomeCompanyNew(OrgId,CompanyId) values(?,?)");

        List params = new ArrayList();
        params.add(ro.getOrgId());
        params.add(ro.getCompanyId());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_OrgIncomeCompanyNew where Id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };

        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

    @Override
    public int remove(Long orgId) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_OrgIncomeCompanyNew where OrgId=?";

        return getJdbcTemplate().update(sql, orgId);
    }



}
